package com.koicarp.university.graduate.service.service.log.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.dao.LogOperateInfoDao;
import com.koicarp.university.graudate.common.dto.log.OperateInfoDto;
import com.koicarp.university.graudate.common.entity.log.LogOperateInfo;
import com.koicarp.university.graduate.service.service.log.ILogOperateInfoService;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import com.koicarp.university.graudate.common.vo.OperateInfoVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【日志管理：操作日志信息表】(LogOperateInfo)表服务实现类
 *
 * @author liutao
 * @since 2020-07-30 17:23:23
 */

@Slf4j
@Service("logOperateInfoService")
@RequiredArgsConstructor
public class LogOperateInfoServiceImpl extends ServiceImpl<LogOperateInfoDao,LogOperateInfo> implements ILogOperateInfoService {

    private final LogOperateInfoDao logOperateInfoDao;

    @Override
    public UniversityResponse queryOperateInfoWithPage(OperateInfoVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<OperateInfoDto> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        boolean isTime = UniversityUtil.isNotNull(vo.getPickerTime());
        if (isTime){
            vo.setBeginTime(vo.getPickerTime()[0]);
            vo.setFinalTime(vo.getPickerTime()[1]);
            //结束时间+1天修正查询范围
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date  d  =  new Date(format.parse(vo.getFinalTime()).getTime()+24*3600*1000);
                vo.setFinalTime(format.format(d));
            } catch (ParseException e) {
                log.error("时间格式不对！！！");
                e.printStackTrace();
            }
        }
        LogOperateInfo logOperateInfo = new LogOperateInfo();
        BeanUtils.copyProperties(vo,logOperateInfo);
        if (!isPage && !findOne){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.logOperateInfoDao.queryAll(logOperateInfo));
            response.data(listRes);
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<OperateInfoDto> result = null;
        result = logOperateInfoDao.queryOperateInfoWithPage(page, vo);
        if (findOne){
            List<OperateInfoDto> list = result.getRecords();
            if (list == null || list.size() == 0){
                log.error("未找到");
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL);
            } else {
                response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
                response.data(list.get(0));
            }
        }else{
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    @Override
    public UniversityResponse deleteListById(List<String> list) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        int i = this.logOperateInfoDao.deleteListById(list);
        if ( i < 0 ){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }


    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogOperateInfo> queryAllByLimit(int offset, int limit) {
        return this.logOperateInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logOperateInfo 实例对象
     * @return 实例对象
     */
    @Override
    public LogOperateInfo insert(LogOperateInfo logOperateInfo) {
        this.logOperateInfoDao.insert(logOperateInfo);
        return logOperateInfo;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public UniversityResponse deleteById(Long id) {
       UniversityResponse response = new UniversityResponse();
       response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
       response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        int i = this.logOperateInfoDao.deleteById(id);
        if(i < 0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_PARAM_WRONG);
        }
        return response;
    }
}